{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Subplots"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It is often useful to compare different subsets of your data side by side. To demonstrate this, we are going to visualize images side by side."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# The ``inline`` flag will use the appropriate backend to make figures appear inline in the notebook.  \n",
    "%matplotlib inline\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# `plt` is an alias for the `matplotlib.pyplot` module\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Load Data\n",
    "\n",
    "The dataset is the digits dataset (from scikit-learn) that I arranged into a csv file for convenience. The data consists of pixel intensity values for 1797 images that are 8 by 8 pixels. This means that the dataset has 64 total values per image. Each image is labeled with a number from 0-9."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load digits dataset\n",
    "filename = 'data/digitsDataset.csv'\n",
    "df = pd.read_csv(filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Columns 0 to 63 are the pixel intensity values for an 8 by 8 image. \n",
    "# label column is what the image is supposed to be. \n",
    "df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Show image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pixel_colnames = df.columns[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pixel_colnames"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get all columns except the label column for the first image\n",
    "image_values = df.loc[0, pixel_colnames].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This is not the correct format for viewing images\n",
    "image_values.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The images are 8 pixels by 8 pixels. It is important to keep in mind that just because a dataset is stored in a certain way, doesnt mean it was meant to be viewed that way. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_values.reshape(8,8)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As it is not easy to understand pixel intensity values by looking at an array, lets visualize the image. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plt.imshow(image_values.reshape(8,8), cmap ='gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Not the correct way to format your data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# this is not the format the image should be in. \n",
    "plt.imshow(image_values.reshape(64, 1), cmap = 'gray')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Subplot Creation\n",
    "We are going to create a 1 by 5 plot. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# There is a large amount of replicated code\n",
    "\n",
    "plt.figure(figsize=(10,2))\n",
    "\n",
    "## The first image\n",
    "plt.subplot(1, 5, 1)\n",
    "image_values = df.loc[0, pixel_colnames].values\n",
    "image_label = df.loc[0, 'label']\n",
    "plt.imshow(image_values.reshape(8,8), cmap ='gray')\n",
    "plt.title('Label: ' + str(image_label))\n",
    "\n",
    "# The second image\n",
    "plt.subplot(1, 5, 2)\n",
    "image_values = df.loc[1, pixel_colnames].values\n",
    "image_label = df.loc[1, 'label']\n",
    "plt.imshow(image_values.reshape(8,8), cmap ='gray')\n",
    "plt.title('Label: ' + str(image_label))\n",
    "\n",
    "# The third image\n",
    "plt.subplot(1, 5, 3)\n",
    "image_values = df.loc[2, pixel_colnames].values\n",
    "image_label = df.loc[2, 'label']\n",
    "plt.imshow(image_values.reshape(8,8), cmap ='gray')\n",
    "plt.title('Label: ' + str(image_label))\n",
    "\n",
    "# The fourth image\n",
    "plt.subplot(1, 5, 4)\n",
    "image_values = df.loc[3, pixel_colnames].values\n",
    "image_label = df.loc[3, 'label']\n",
    "plt.imshow(image_values.reshape(8,8), cmap ='gray')\n",
    "plt.title('Label: ' + str(image_label))\n",
    "\n",
    "# The fifth image\n",
    "plt.subplot(1, 5, 5)\n",
    "image_values = df.loc[4, pixel_colnames].values\n",
    "image_label = df.loc[4, 'label']\n",
    "plt.imshow(image_values.reshape(8,8), cmap ='gray')\n",
    "plt.title('Label: ' + str(image_label))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Using a for loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# range(0,5) produces a sequence of integers from 0\n",
    "# up to but not including 5\n",
    "list(range(0,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This is a lot less code\n",
    "\n",
    "plt.figure(figsize=(10,2))\n",
    "for index in range(0, 5):\n",
    "\n",
    "    plt.subplot(1, 5, 1 + index )\n",
    "    image_values = df.loc[index, pixel_colnames].values\n",
    "    image_label = df.loc[index, 'label']\n",
    "    plt.imshow(image_values.reshape(8,8), cmap ='gray')\n",
    "    plt.title('Label: ' + str(image_label))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
